package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.pojo.Author;
import com.ssm.pojo.Manager;
import com.ssm.pojo.Msg;
import com.ssm.service.AuthorService;
import com.ssm.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ManagerAuthorController {

    @Autowired
    private AuthorService authorService;

    @Autowired
    private ManagerService managerService;

    @RequestMapping("/toShowAuthor")
    public String toShowAuthor(){
        return "showAuthor";
    }

    @RequestMapping(value = "/showAuthor", method = RequestMethod.GET)
    @ResponseBody
    public Msg showManagers(@RequestParam(value = "pn", defaultValue = "1") int pn,
                            @RequestParam(value = "select_value", defaultValue = "1") int value,
                            @RequestParam(required = false) String find_value) {
        /*
         * 在查询之前只需要调用，传入页码。以及每页的大小
         * */
        if (find_value == "undefined") {
            find_value = "";
        }
        PageHelper.startPage(pn, 5);
        if (value == 1) {
            List<Author> list = authorService.authorList(null);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 2) {
            List<Author> list = authorService.find_name(find_value);
            PageInfo page = new PageInfo(list);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 3) {
            List<Author> list = authorService.find_cdorm(find_value);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else {
            return Msg.fail().add("value", value);
        }
    }

    @RequestMapping(value = "/addAuthor",method=RequestMethod.POST)
    @ResponseBody
    public Msg addAuthor(Author author){
        if(author==null){
            return Msg.fail().add("msg","提交数据不能为空！");
        }
        try {
            authorService.insertByAuthor(author);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().add("msg","系统异常！");
        }
        return Msg.success().add("msg","权限添加成功！");
    }

    @RequestMapping(value = "/getManagerAmount",method=RequestMethod.POST)
    @ResponseBody
    public Msg getManagerAmount(String amount){
        Manager manager;
        try {
            manager = managerService.query(amount);
            if(manager==null){
                return Msg.fail().add("msg","账号不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().add("msg","系统异常！");
        }
        return Msg.success().add("msg","权限添加成功！").add("author",manager);
    }
    @RequestMapping(value = "/getAuthorAmount",method=RequestMethod.POST)
    @ResponseBody
    public Msg getAuthorAmount(String amount){
        if(amount==null||amount==""){
            return Msg.fail().add("msg","账号不能为空");
        }
        List<Author> author;
        try {
            author= authorService.authorList(amount);
            if(author.size()==0){
                return Msg.fail().add("msg","权限不足！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().add("msg","系统异常！");
        }
        return Msg.success().add("msg","权限添加成功！").add("author",author);
    }

    @ResponseBody
    @RequestMapping(value = "/authorUpdate", method = RequestMethod.POST)
    public Msg authorUpdate(Author author) {
        try {
            authorService.authorUpdate(author);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().add("msg","系统错误！");
        }
        return Msg.success().add("msg","修改成功");
    }
    @ResponseBody
    @RequestMapping(value = "/authorDel", method = RequestMethod.POST)
    public Msg authorDel(String amount) {
        List<String> list = new ArrayList<String>();
        if (amount.contains("-")) {
            String[] str_all = amount.split("-");
            for (String str : str_all) {
                list.add(str);
            }
            authorService.deleteAuthorList(list);
        } else {
            authorService.deleteByAmount(amount);
        }
        return Msg.success();
    }
}
